package com.chen.nongansampling.activity;

import android.graphics.Color;
import android.os.Bundle;
import android.util.Log;
import android.view.View;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.Toolbar;
import androidx.fragment.app.Fragment;
import androidx.fragment.app.FragmentStatePagerAdapter;
import androidx.viewpager.widget.ViewPager;

import com.chen.nongansampling.R;
import com.chen.nongansampling.adapter.NoticeActivityFragmentAdapter;
import com.chen.nongansampling.app.BaseApplication;
import com.chen.nongansampling.bean.SimpleRequest;
import com.chen.nongansampling.bean.notice.NoticeBean;
import com.chen.nongansampling.fragment.notice.NoticeInfoFragment;
import com.chen.nongansampling.fragment.notice.NoticeKnowledgeFragment;
import com.chen.nongansampling.fragment.notice.NoticePushFragment;
import com.chen.nongansampling.model.CallbackData;
import com.chen.nongansampling.presenter.NoticePresenter;
import com.chen.nongansampling.presenterView.NoticeView;
import com.google.android.material.tabs.TabLayout;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;

public class NoticeActivity extends AppCompatActivity implements NoticeView {
    @BindView(R.id.tab_layout)
    TabLayout tabLayout;
    @BindView(R.id.myViewPager)
    ViewPager myViewPager;


//    @BindView(R.id.tl)
//    TabLayout t1;
//    @BindView(R.id.vp)
//    ViewPager vp;
//    private TabFragmentAdapter adapter;
//    private TextView tabText1;
//    private TextView tabText2;
//    private TextView tabText3;
//    String[] titles = {"商品", "详情", "评论"};

    private ArrayList<String> tab_title_list = new ArrayList<>();//存放标签页标题
    private ArrayList<Fragment> fragment_list = new ArrayList<>();//存放ViewPager下的Fragment
    private Fragment fragment1, fragment2, fragment3, fragment4;
    private FragmentStatePagerAdapter adapter;//适配器


    private ArrayList<String> fragmentInfoData = new ArrayList<>();//存放标签页标题
    private ArrayList<String> fragmentPushData = new ArrayList<>();//存放标签页标题
    private ArrayList<String> fragmentKnowledgeData = new ArrayList<>();//存放标签页标题




    //  private TabLayout tabLayout;


    NoticeInfoFragment noticeInfoFragment;
    NoticePushFragment noticePushFragment;
    NoticeKnowledgeFragment noticeKnowledgeFragment;
    //    @BindView(R.id.webview)
//    WebView webview;
    private NoticePresenter noticePresenter = new NoticePresenter(getBaseContext());

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_notice);
        ButterKnife.bind(this);
        noticePresenter.BindPresentView(this);
        SimpleRequest simpleRequest = new SimpleRequest();
        simpleRequest.setAccountPkId(BaseApplication.getAccount().getPkId());
        noticePresenter.getNoticeResponseInfo(simpleRequest);
initToolbar();

//        t1 = (TabLayout) findViewById(R.id.tl);
//        vp = (ViewPager) findViewById(R.id.vp);
        //初始化ViewPager
//        initViewPager();
//        //初始化TabLayout
//        initTabLayout();


     //   tabLayout = (TabLayout) findViewById(R.id.my_tablayout);
      //  viewPager = (ViewPager) findViewById(R.id.my_viewpager);
    }
    private  void initToolbar(){
        Toolbar toolbar = (Toolbar)findViewById(R.id.toolbar);
        setSupportActionBar(toolbar);
        getSupportActionBar().setDisplayShowTitleEnabled(false);//隐藏标题
        toolbar.setNavigationIcon(R.mipmap.back_white);       //加载图标
        toolbar.setTitle("");
        toolbar.setNavigationOnClickListener(new View.OnClickListener() {   //为图标设置监听器
            @Override
            public void onClick(View v) {
                finish();
            }
        });
    }


    @Override
    protected void onStart() {
        super.onStart();



    }

    @Override
    public void getNoticeList(Object resultData) {

       // CallbackData<List<NoticeBean>> noticeList = (CallbackData<List<NoticeBean>>) resultData;




        CallbackData<NoticeBean> callbackData= (CallbackData<NoticeBean>) resultData;

        List<NoticeBean> temp=(List<NoticeBean>)((CallbackData<NoticeBean>) resultData).getData();
List<NoticeBean> list=new ArrayList<>();
for(int i=0;i<temp.size();i++)
{
    if(temp.get(i).getContent()!=null)
    {
        list.add(temp.get(i));

    }
}

        for(int i=0;i<list.size();i++)
        {
            if(list.get(i).getTitle()!=null){
                if(list.get(i).getTitle().length()>6){
                    String str = "...";
                    StringBuilder sb = new StringBuilder(list.get(i).getTitle());
                    System.out.println(list.get(i).getContent());
                    System.out.println(list.get(i).getTitle());
                    System.out.println(list.get(i).getTitle().length());
                    sb.replace(5, temp.get(i).getTitle().length()-1, str );
                    list.get(i).setTitle(sb.toString());
                }
            }
        }

        List<NoticeBean> list0=new ArrayList<>();
        List<NoticeBean> list1=new ArrayList<>();
        List<NoticeBean> list2=new ArrayList<>();
        for(int i=0;i<list.size();i++)
        {
            System.out.println(list.get(i).getType());
            if("0".equals(list.get(i).getType())){
                list0.add(list.get(i));

            }
            if("1".equals(list.get(i).getType())){
                list1.add(list.get(i));

            }
            if("2".equals(list.get(i).getType())){
                list2.add(list.get(i));

            }
        }


        tabLayout.setupWithViewPager(myViewPager);//将TabLayout与Viewpager联动起来
        //           tabLayout.setTabsFromPagerAdapter(adapter);//给TabLayout设置适配器
        tabLayout.setSelectedTabIndicatorColor(Color.rgb(66, 142, 254));

        fragment1 = new NoticeKnowledgeFragment(list0);
        fragment2 = new NoticePushFragment(list1);
   //     fragment3 = new NoticeInfoFragment((List<NoticeBean>) callbackData.getData());
        fragment3 = new NoticeInfoFragment(list2);
        fragment_list.clear();
        fragment_list.add(fragment1);
        fragment_list.add(fragment2);
        fragment_list.add(fragment3);


        tab_title_list.add("通知信息");
        tab_title_list.add("推送信息");
        tab_title_list.add("知识库");

        tabLayout.addTab(tabLayout.newTab().setText(tab_title_list.get(0)));
        tabLayout.addTab(tabLayout.newTab().setText(tab_title_list.get(1)));
        tabLayout.addTab(tabLayout.newTab().setText(tab_title_list.get(2)));

        adapter = new NoticeActivityFragmentAdapter(getSupportFragmentManager(), tab_title_list, fragment_list);
        myViewPager.setOffscreenPageLimit(4);
        myViewPager.setAdapter(adapter);//给ViewPager设置适配器





     //   if(temp.get(i).getTitle().length()>6){
     //       String str = "...";
     //       StringBuilder sb = new StringBuilder(temp.get(i).getTitle());
     //       sb.replace(5, temp.get(i).getTitle().length()-1, str );
     //       temp.get(i).setTitle(sb.toString());
     //       list.add(temp.get(i));
     //   }












//       String s = new Base64.Encoder()

//        if (noticeList.getData().get(1) != null) {
//            String str2 = new String(Base64.decode(noticeList.getData().get(1).getContent().getBytes(), Base64.DEFAULT));
//            Log.d("html", str2);
//
//            webview.getSettings().setJavaScriptEnabled(true);
//
//            webview.loadData(str2, "t ext/html", "UTF-8");
//        }
    }

    @Override
    public void onError(String result) {
        Log.d("htmlerror", result);

    }

    @Override
    public void onSuccess(Object resultData) {










    }

//    public void initViewPager() {
//        adapter = new TabFragmentAdapter(getSupportFragmentManager(), titles);
//        vp.setAdapter(adapter);
//        //ViewPager点击事件
//        vp.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
//            @Override
//            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//
//            }
//
//            @Override
//            public void onPageSelected(int position) {
//                //选中ViewPager与TabLayout联动
//                t1.getTabAt(position).select();
//            }
//
//            @Override
//            public void onPageScrollStateChanged(int state) {
//
//            }
//        });
//
//    }
//
//    public void initTabLayout() {
//        t1.setTabMode(TabLayout.MODE_SCROLLABLE);//模式，可滑动，默认是不可滑动
//        t1.setSelectedTabIndicatorColor(getResources().getColor(android.R.color.holo_blue_dark));//指示器颜色
//        //   t1.setSelectedTabIndicatorHeight((int) getResources().getDimension(R.dimen.indicatorHeight));//指示器高度
//        t1.setSelectedTabIndicatorHeight(20);//指示器高度
//
//        t1.addTab(t1.newTab().setCustomView(R.layout.tabview_main));  //这一段是可以改写简单点.可以借鉴第二种方式自定义标签布局
//        tabText1= (TextView) findViewById(R.id.my_tablayout);
//        tabText1.setText("商品");
//        t1.addTab(t1.newTab().setCustomView(R.layout.tabview_main));
//        tabText2= (TextView) findViewById(R.id.my_tablayout);
//        tabText2.setText("详情");
//        t1.addTab(t1.newTab().setCustomView(R.layout.tabview_main));
//        tabText3= (TextView) findViewById(R.id.my_tablayout);
//        tabText3.setText("评论");
//        //TabLayout点击事件
//        t1.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
//            //选中tab
//            @Override
//            public void onTabSelected(TabLayout.Tab tab) {
//                //选中tab时,TabLayout与ViewPager联动
//                vp.setCurrentItem(tab.getPosition());
//                switch (tab.getPosition()) {
//                    case 0:
//                        tabText1.setTextColor(getResources().getColor(R.color.colorAccent));
//                        break;
//                    case 1:
//                        tabText2.setTextColor(getResources().getColor(R.color.colorAccent));
//                        break;
//                    case 2:
//                        tabText3.setTextColor(getResources().getColor(R.color.colorAccent));
//                        break;
//                }
//            }
//
//            //未选择tab
//            @Override
//            public void onTabUnselected(TabLayout.Tab tab) {
//
//            }
//
//            //重复选中tab
//            @Override
//            public void onTabReselected(TabLayout.Tab tab) {
//
//            }
//        });
//
//    }
}





